/**
 * Created by IntelliJ IDEA. User: Liu Yuhong Date: 12-10-31 Time: 下午4:59 Drop
 * menu plugin
 */
define(function(require, exports, module) {
	var meta = require("../../package.json");
	if (meta.theme) {
		var themes = meta.theme;
		for (var i = 0; i < themes.length; i++) {
			var cssurl = '../theme/' + themes[i] + '/css/drop_menu.css';
			require.async(cssurl);
		}
	}
	var dropMenu_tpl = require("./drop_menu.tpl");
	var Widget = require("core/widget/dist/widget");
	var DropMenu = Widget.extend({
		//初始化参数
		initCustAttr : function() {
			//使用set给attr设置属性
			this.set("listWidth", "width: " + (this.get("itemWidth") + 2)
							+ "px; ");
			this.set("menuWidth", "width: " + (this.get("itemWidth") + 10)
							+ "px; ");
			this.set("lineWidth", "width: " + this.get("itemWidth") + "px; ");
			this.set("lineHeight", "height: " + this.get("itemHeight")
							+ "px; lineHeight: " + this.get("itemHeight")
							+ "px; ");
		},
		//加载数据
		loadData : function() {
			var ui = this;
			var url = this.get("url");
			$.ajax({
				type : 'POST',
				url : url,
				success : function(data) {
					ui.set("listHeight", "height: "
									+ (ui.get("itemHeight") * data.length)
									+ "px");
					ui.set("borderHeight",
									"height: "
											+ (ui.get("itemHeight")
													* data.length + 14) + "px");
					for (var loop = 0; loop < data.length; loop++) {
						data[loop]["lineWidth"] = "width: " + ui.get("itemWidth") + "px; ";
					}
                    console.log(data);
					ui.setModel(data);
                    for (loop = 0; loop < data.length; loop++) {
                        $( ui.element.find("a")[loop] )
                            .bind('click', eval(data[loop].callback))
                            .bind('click', function() {
                                ui.element.hide();
                                return false;
                            });
                    }
				},
				dataType : 'json'
			});
		},
		bindEvent:function(){
			console.log(this.element);
		},
		template : dropMenu_tpl
	});
	module.exports = DropMenu;
});